package com.bbdjt.weixin.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.bbdjt.weixin.model.Admin;



/**
 * ClassName: SessionUtil 
 * @Description: TODO
 * @author guoLiang
 * @date 2016年3月31日
 */
public class SessionUtil {
	
	
	//验证权限
	/*public static boolean verifyPermission(String actionName, String firstCode)
			throws Exception {
		boolean t = false;
		@SuppressWarnings("rawtypes")
		Map mp = (Map) ActionContext.getContext().getSession()
				.get(Constants.SESSION_USER_PERMISSION);
		if (null != mp
				&& null != mp.get(actionName)
				&& ("/" + mp.get(actionName).toString() + "/").indexOf("/"
						+ firstCode + "/") > -1) {
			t = true;
		}
		return t;
	}
*/
	//许可
	/*public static void setPermission(RoleService roleService, String roleIds)
			throws Exception {
		Map m = new HashMap();
		if (!StringUtil.isEmpty(roleIds)) {
			roleIds = roleIds.substring(1, roleIds.length() - 1);
			String[] roleId = roleIds.split(",");
			for (int i = 0; i < roleId.length; i++) {
				if (!StringUtil.isEmpty(roleId[i])) {
					Role role = roleService
							.getRole(Integer.parseInt(roleId[i]));
					m = setPermissionMap(role, m);
				}
			}
			if (null != m) {
				ActionContext context = ActionContext.getContext();
				context.getSession().put(Constants.SESSION_USER_PERMISSION, m);
			}
		}

	}*/

	
	/*private static Map setPermissionMap(Role role, Map m) {
		try {
			if (null != role && role.getLimits().trim().length() > 0) {
				m = JSON.parseObject(role.getLimits().trim().toUpperCase());
			}
		} catch (Exception e) {

		}
		return m;
	}*/

	public static void setSession(HttpSession session,List<Admin> admin) throws Exception {
		session.setAttribute(Constants.SESSION_USERINFO_ENTITY,admin.get(0));
	}
	
	public static void setSession(HttpSession session,Admin admin) throws Exception {
		session.setAttribute(Constants.SESSION_USERINFO_ENTITY, admin);
	}
	
	public static Admin getSession(HttpSession session) throws Exception {
		return null==session.getAttribute(Constants.SESSION_USERINFO_ENTITY)?null:(Admin)session.getAttribute(Constants.SESSION_USERINFO_ENTITY);
	}

	public static void logout(HttpSession session) throws Exception {
		session.removeAttribute(Constants.SESSION_USERINFO_ENTITY);
		session.removeAttribute(Constants.SESSION_USER_PERMISSION);
	}
}
